/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com" .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hh.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hh.droid.R;
import com.hh.listeners.OnItemSpinnerClickListener;
import com.hh.listeners.OnPopupMenuItemClickListener;

/**
 * 
 * A custom Spinner with holo light style
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */
public class UiSpinner extends RelativeLayout implements OnClickListener,
		OnPopupMenuItemClickListener {

	private UiPopupMenu _mMenu;
	private int _mBackgroundColor = -1;
	private TextView _mTextView;
	private OnItemSpinnerClickListener _mItemPopupMenuClickListener;
	
	public UiSpinner(Context context, AttributeSet pAttributes) {
		super(context, pAttributes);
		_mBackgroundColor = getResources().getColor(R.color.holo_gray_lev0);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.ui_spinner, this);
		RelativeLayout llLayout = (RelativeLayout) findViewById(R.id.llLayout);
		if (llLayout != null) {
			_mTextView = (TextView) llLayout.getChildAt(0);
			llLayout.setOnClickListener(UiSpinner.this);
		}
	}

	/**
	 * <br>
	 * Used to set background menu color
	 * 
	 * @author WajdiHh
	 * @param pColor
	 */
	public void setBackgroundPopupMenuColor(int pColor) {
		_mBackgroundColor = pColor;
		if (_mMenu != null)
			_mMenu.setBackgroundColor(_mBackgroundColor);
	}

	/**
	 * <br>
	 * Returns a string with the same characters in the same order as in this
	 * sequence.
	 * 
	 * @author WajdiHh
	 * @return : text selection
	 */
	public String getTextSelection() {
		return _mMenu.getTextSelection();
	}

	/**
	 * <br>
	 * Set text size of the item
	 * 
	 * @author WajdiHh
	 * @param pSize
	 */
	public void setTextSize(float pSize) {
		_mTextView.setTextSize(pSize);
	}

	/**
	 * <br>
	 * Set DEFAULT item selection
	 * 
	 * @author WajdiHh
	 * @param pPos :position of the selected item
	 */
	public void setSelection(int pPosition) {
		if (_mMenu != null) {
			_mMenu.setSelectionPosition(pPosition);
			_mTextView.setText(_mMenu.getTextSelection());
			onPopupMenuItemClick(getRootView(), pPosition, 0);
		}
	}
	/**
	 * <br>
	 * Set the text selection on the spinner
	 * 
	 * @author WajdiHh
	 * @param pText
	 */
	public void setTextSelection(String pText){
		if (_mMenu != null) {
			int lPos=_mMenu.getPosFromText(pText);
			if(lPos!=-1){
				setSelection(lPos);
			}
		}
	}
	/**
	 * <br>
	 * Set Spinner popupMenu
	 * 
	 * @author WajdiHh
	 * @param pPopUpmenu
	 */
	public void setPopupMenu(UiPopupMenu pPopUpmenu) {
		_mMenu = pPopUpmenu;
		if (_mBackgroundColor == -1)
			_mMenu.setBackgroundColor(_mBackgroundColor);

		_mMenu.setOnPopupMenuItemClickListener(this);
		_mTextView.setText(_mMenu.getTextSelection());

	}
	/**
	 * <br>
	 * Register a callback to be invoked when when popup menu item is clicked
	 * 
	 * @author WajdiHh
	 * @param pListener : the listener to handle item  click
	 * @see OnItemSpinnerClickListener
	 */
	public void setOnItemPopupMenuClickListener(OnItemSpinnerClickListener pListener) {
		_mItemPopupMenuClickListener = pListener;
		onPopupMenuItemClick(_mTextView, 0,0);
	}
	
	/**
	 * <br>
	 * Used to set Menu Items listener
	 * if pDisableOnItemClickListenerFirstBuild is a false , we will not pass into onPopupMenuItemClick method on first build
	 * @param pListener
	 * 
	 * @author WajdiHh
	 * @see OnItemSpinnerClickListener
	 * @param pDisableOnItemClickListenerFirstBuild : if false we will not pass into onPopupMenuItemClick
	 */
	public void setOnItemPopupMenuClickListener(boolean pDisableOnItemClickListenerFirstBuild ,OnItemSpinnerClickListener pListener) {
		_mItemPopupMenuClickListener = pListener;
		if(pDisableOnItemClickListenerFirstBuild)
			onPopupMenuItemClick(_mTextView, 0,0);
	}

	@Override
	public void onClick(View v) {

		if (_mMenu != null) {
			int[] lLocation = new int[2];
			getLocationOnScreen(lLocation);
			_mMenu.setWidth(this.getWidth());

			int lX = this.getWidth() - _mMenu.getWidth();
			int lY = this.getHeight() + 2;
			Point p = new Point(lLocation[0] + lX, lLocation[1] + lY);
			_mMenu.setPositionOnScreen(p);
			_mMenu.show();
		}

	}

	@Override
	public void onPopupMenuItemClick(View v, int position,int id) {
		if (_mItemPopupMenuClickListener != null) {
			_mItemPopupMenuClickListener.onItemClick(this, v, position);
		}
		_mTextView.setText(_mMenu.getTextSelection());
	}
}
